package com.search.config;


import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

import java.net.URL;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 窗体启动前的加载动画
 *
 * @Author: huanmin
 * @Date: 2022/11/25 15:00
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class StartLoadFroms {
    Stage stage;
    Stage mainStage;
    AnchorPane anchorPane;//创建空白面板
    Scene scene;
    VBox content;//创建垂直布局
    ImageView imageView;
    double width;
    double height;
    String imageUrl;

    public StartLoadFroms(double width, double height, String imageUrl, Stage mainStage) {
        this.stage = new Stage();
        this.anchorPane = new AnchorPane();
        this.content = new VBox();
        this.width = width;
        this.height = height;
        this.imageUrl = imageUrl;
        this.mainStage = mainStage;
        init();

    }

    private void init() {
        config();//窗体样式和配置
        tool();//窗体内部控件
    }

    private void config() {
        //将空白面板设置为场景
        this.scene = new Scene(anchorPane, width, height);
        //设置场景背景透明,不然anchorPane设置圆角会把场景的背景显示出来
        scene.setFill(Color.valueOf("#00000000"));
        //将场景添加到面板
        stage.setScene(scene);
        //窗口小部件清除
        stage.initStyle(StageStyle.TRANSPARENT);
    }

    private void tool() {
        imageView();
    }

    private void imageView() {
        URL url = getClass().getResource(imageUrl);
        Image image = new Image(url.toExternalForm());
        imageView = new ImageView(image);
        imageView.setFitWidth(width);
        imageView.setFitHeight(height);
        anchorPane.getChildren().add(imageView);
    }




    public void show() {
        stage.show();
        //这里默认延迟500毫秒后检测主窗体是否加载完成,加载完成后,关闭启动窗体  ,如果后期启动慢这个可以改为1毫秒
        Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event1) {
                mainStage.setOpacity(0);//设置主窗体透明度
                mainStage.show();//显示主窗体
                long lastTimerCallms = System.nanoTime();
                AtomicLong lastTimerCall = new AtomicLong(lastTimerCallms);
                //检测主窗体是否加载完成 ,加载完成后,关闭启动窗体
                if (mainStage.isShowing()) {
                    //stage逐渐消失
                    AnimationTimer timer1 = new AnimationTimer() {
                        @Override
                        public void handle(long now) {
                            if (now > lastTimerCall.get() + 30000000) { // 30 ms
                                if (stage.getOpacity() < 0.1) {
                                    stage.close();
                                    this.stop();
                                } else {
                                    //stage逐渐消失
                                    stage.setOpacity(stage.getOpacity() - 0.1);
                                    lastTimerCall.set(now);
                                }
                            }
                        }
                    };
                    timer1.start();
                    //stage关闭后,主窗体逐渐显示
                    stage.setOnHidden(event -> {
                        AnimationTimer timer2 = new AnimationTimer() {
                            @Override
                            public void handle(long now) {
                                if (now > lastTimerCall.get() + 30000000) { // 30 ms
                                    if (mainStage.getOpacity() > 0.9) {
                                        this.stop();
                                    } else {
                                        //主窗体逐渐显示
                                        mainStage.setOpacity(mainStage.getOpacity() + 0.1);
                                        lastTimerCall.set(now);
                                    }
                                }
                            }
                        };
                        timer2.start();
                    });
                }
            }
        }));
        fiveSecondsWonder.setCycleCount(1);//循环执行Timeline.INDEFINITE   或者设置次数
        fiveSecondsWonder.play();
    }
}